<div class="box box-info">
  <div class="box-header with-border">
    <h3 class="box-title">添加机构部门信息</h3>
    <div class="box-tools">
      <span onclick="HtmlRender('${BASE}/admin/sys/SysDept/index')" class="btn btn-info btn-sm">列表</span>
    </div>
  </div>
 
  <form action="${BASE}/admin/sys/SysDept/save" id="form_save" method="post" class="form-horizontal">
  <div class="box-body">
  
      <input type="hidden" id="parentId" name="parentId" value="${parentDept.id}" />
      <input type="hidden" id="level" name="level" value="${parentDept.level+1}" />
      <div class="form-group">
        <label for="parentId" class="col-sm-3 control-label">上级机构部门：</label>
        <div class="col-sm-6">
        <div class="input-group">
          <input type="text" id="parentName" name="parentName" value="${parentDept.name}" class="form-control" readonly="readonly" >
          <span class="input-group-btn" id="btn_span_parentIds"><button type="button" class="btn btn-info btn-flat"><i class="fa fa-th"></i> 选择上级机构</button></span>
        </div>
        </div>
        <div class="col-sm-3"> </div>
      </div>
      <div class="form-group">
        <label for="name" class="col-sm-3 control-label">* 机构部门名称：</label>
        <div class="col-sm-6">
          <input type="text" id="name" name="name" value="" class="form-control">
        </div>
        <div class="col-sm-3"> </div>
      </div>
      <div class="form-group">
        <label for="address" class="col-sm-3 control-label">联系地址：</label>
        <div class="col-sm-6">
          <input type="text" id="address" name="address" value="" class="form-control">
        </div>
        <div class="col-sm-3"> </div>
      </div>
      <div class="form-group">
        <label for="zipCode" class="col-sm-3 control-label">邮政编码：</label>
        <div class="col-sm-6">
          <input type="text" id="zipCode" name="zipCode" value="" class="form-control">
        </div>
        <div class="col-sm-3"> </div>
      </div>
      <div class="form-group">
        <label for="master" class="col-sm-3 control-label">负责人：</label>
        <div class="col-sm-6">
          <input type="text" id="master" name="master" value="" class="form-control">
        </div>
        <div class="col-sm-3"> </div>
      </div>
      <div class="form-group">
        <label for="phone" class="col-sm-3 control-label">电话：</label>
        <div class="col-sm-6">
          <input type="text" id="phone" name="phone" value="" class="form-control">
        </div>
        <div class="col-sm-3"> </div>
      </div>
      <div class="form-group">
        <label for="fax" class="col-sm-3 control-label">传真：</label>
        <div class="col-sm-6">
          <input type="text" id="fax" name="fax" value="" class="form-control">
        </div>
        <div class="col-sm-3"> </div>
      </div>
      <div class="form-group">
        <label for="email" class="col-sm-3 control-label">邮箱：</label>
        <div class="col-sm-6">
          <input type="text" id="email" name="email" value="" class="form-control">
        </div>
        <div class="col-sm-3"> </div>
      </div>

      <div class="form-group">
        <label class="col-sm-3 control-label">备注信息：</label>
        <div class="col-sm-6">
          <textarea id="remarks" name="remarks" cols="" rows="" class="form-control"  placeholder="备注信息...255个字符以内" dragonfly="true" onKeyUp="textarealength(this,255)"></textarea>
          <p class="textarea-numberbar"><em class="textarea-length">0</em>/255</p>
        </div>
        <div class="col-sm-3"> </div>
      </div>
      
  </div>
  <div class="box-footer">
    <div class="col-sm-10 col-sm-offset-2">
      <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;保存&nbsp;&nbsp;">
    </div>
  </div>
  </form>

  <div id="div_dept_select_tree" class="content" style="display: none;">
    <div class="box" style="height: 350px">
      <div class="box-body">
        <ul id="ul_dept_select_tree" class="ztree"></ul>
      </div>
    </div>
  </div>
</div>
  
<script type="text/javascript">
$(function(){

	var deptTree;
	
	var tree_setting = {
		callback: {
			onClick: function(event, treeId, treeNode, clickFlag) {
			}
		}
	};
	
	
	function showDailog(){
		if(!deptTree){
			$("#ul_dept_select_tree").html("<li>没有机构部门数据</li>");
		}
		layer.open({
			type: 1,
			shadeClose:true,
			title :'选择上级机构',
			area: ['500px', '500px'],
			content: $('#div_dept_select_tree'),
			btn: ['确认选择', '取消', '设置为顶级机构'],
			btn1: function(index, layero){
				if(deptTree){
					var nodes = deptTree.getSelectedNodes()[0];
					if(nodes){
						$("#form_save #parentId").val(nodes.id);
						$("#form_save #level").val(parseInt(nodes.level)+1);
						$("#form_save #parentName").val(nodes.name);
					}
				};
				layer.close(index);
			},
			btn2: function(index, layero){}, 
			btn3: function(index){
				$("#form_save #parentId").val(0);
				$("#form_save #level").val(0);
				$("#form_save #parentName").val('顶级机构');
			}
		});
	}
	function select_parent_dept(){
		if(deptTree){
			showDailog();
		}else{
			$.getJSON("${BASE}/admin/sys/SysDept/deptSelectTree",{}, function(str){
				$("#ul_dept_select_tree").text("");
				if(str)
					deptTree = $.fn.zTree.init($("#ul_dept_select_tree"), tree_setting, str);
				else
					deptTree = null;
				showDailog();
			});
		}
	}
	
	$("#form_save #parentName").click(select_parent_dept);
	$("#form_save #btn_span_parentIds").click(select_parent_dept);
	$("#form_save").validate({
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		submitHandler : function(form) {
			layer.load(1, {shade: [0.3,'#fff']});
			$(form).ajaxSubmit({
				dataType:"json",
				success:function( jsondata ){
					layer.closeAll('loading');
					if( jsondata.code == "001" ){
						layer.msg(jsondata.msg, {icon: 6,offset: '45px'});
						HtmlRender("${BASE}/admin/sys/SysDept/index");
					}else{
						layer.msg(jsondata.msg, {icon: 5,offset: '45px'});
					}
				}
			});
		},
		rules : {
			name : {
				required : true
			}
		}, 
		messages : { 
			name : { 
				required : "名称不能为空"
			}
		}
	});
});
</script>